//
//  SecondViewController.m
//  DriveStudy
//
//  Created by chen on 2017/4/12.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "SecondViewController.h"

#define CellIdentifier @"SecondTableViewCell"

@interface SecondViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_tableView;
    NSArray *_listArray;
}

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = _barTitle;
    [self createTableView];
    [self reloadData];
}

-(void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
}
-(void)reloadData{
    FirstLevelBL *bl = [FirstLevelBL new];
    _listArray=[bl findAll:_number];
}

#pragma mark - tableview协议方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _listArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80.0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] lastObject];
        //设置圆角
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.numberLabel.layer.masksToBounds = YES;
        cell.numberLabel.layer.cornerRadius = 8;
    }
    if (_number == 1) {
        FirstLevelModel *firstModel = _listArray[indexPath.row];
        
        cell.numberLabel.text = firstModel.pid;
        cell.titleLable.text = firstModel.pname;
    }else if (_number == 3){
        SecondLevelModel *second = _listArray[indexPath.row];
        cell.numberLabel.text = second.serial;
        cell.titleLable.text = second.sname;
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AnswerViewController *answer = [[AnswerViewController alloc]init];
//    answer.navigationItem.leftBarButtonItem.title = @"";
//    FirstLevelModel *firstModel = _listArray[indexPath.row];
    if (_number == 1) {
        answer.number = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
        answer.type = 1;
    }else if(_number == 3){
        SecondLevelModel *second = _listArray[indexPath.row];
        answer.type = 4;
        answer.number = second.sid;
    }
    [self.navigationController pushViewController:answer animated:YES];
}


@end
